package com.gzz.orchid.common.core.model;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.Objects;


/**
 * 查询请求分页参数
 *
 * @author higmd
 */
@Slf4j
public abstract class QueryParam {
    //所属用户
    private Long ownerId;
    //是否删除
    private Integer deleted = 0;
    // 搜索关键字
    private String searchKey;
    // 日期类型
    private String dateType;
    // 开始时间
    private String beginDate;
    // 结束时间
    private String endDate;
    // 分页信息
    private Pagination pager = new Pagination();

    /**
     * 参数转为查询
     *
     * @return
     */
    public QueryWrap toQueryWrap() {
        QueryWrap paramMap = new QueryWrap();
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                if (!"pager".equalsIgnoreCase(field.getName())) {
                    field.setAccessible(true);
                    if (Objects.nonNull(field.get(this))) {
                        paramMap.put(field.getName(), field.get(this));
                    }
                    field.setAccessible(false);
                }
            } catch (IllegalAccessException e) {
                log.error(e.getLocalizedMessage());
                log.error(e.getMessage());
            }
        }
        //
        if (Objects.nonNull(ownerId)) {
            paramMap.put("ownerId", ownerId);
        }
        if (Objects.nonNull(deleted)) {
            paramMap.put("deleted", deleted);
        }
        if (Objects.nonNull(searchKey)) {
            paramMap.put("searchKey", searchKey);
        }
        if (Objects.nonNull(dateType)) {
            paramMap.put("dateType", dateType);
        }
        if (Objects.nonNull(beginDate)) {
            paramMap.put("beginDate", beginDate);
        }
        if (Objects.nonNull(endDate)) {
            paramMap.put("endDate", endDate);
        }
        return paramMap;
    }

    public Pagination getPager() {
        return pager;
    }

    public void setPager(Pagination pager) {
        this.pager = pager;
    }

    public String getSearchKey() {
        return this.searchKey;
    }

    public void setSearchKey(String searchKey) {
        this.searchKey = searchKey;
    }

    public String getDateType() {
        return dateType;
    }

    public void setDateType(String dateType) {
        this.dateType = dateType;
    }

    public String getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(String beginDate) {
        this.beginDate = beginDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public Integer getDeleted() {
        return deleted;
    }

    public void setDeleted(Integer deleted) {
        this.deleted = deleted;
    }

    public Long getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(Long ownerId) {
        this.ownerId = ownerId;
    }
}
